package team.seekers.catering.user.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import team.seekers.catering.common.enums.GradeEnum;
import team.seekers.catering.common.enums.RoleEnum;
import team.seekers.catering.common.utils.CateringUtil;
import team.seekers.catering.user.entity.dao.Building;
import team.seekers.catering.user.entity.dao.User;
import team.seekers.catering.user.entity.dto.update.OnlineUserUpdateDTO;
import team.seekers.catering.user.entity.dto.update.UserShowInfoUpdateDTO;
import team.seekers.catering.user.entity.vo.info.UserInfoVO;
import team.seekers.catering.common.handler.exceptionHandler.ResultException;
import team.seekers.catering.user.mapper.UserMapper;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper, User>{


    @Autowired
    private BuildingService buildingService;

    /**
     * 绑定身体数据成为线上用户
     */
    public void bindOnline(OnlineUserUpdateDTO onlineUserUpdateDTO) {
        String userId = CateringUtil.getUserId();
        User user = getById(userId);
        if(user == null){
            throw new ResultException().message("你已掉线，请重新登录");
        }

        BeanUtils.copyProperties(onlineUserUpdateDTO, user);
        if(user.getRole().getValue()<RoleEnum.USER_ONLINE.getValue())
            user.setRole(RoleEnum.USER_ONLINE);
        boolean b = updateById(user);
        if(!b){
            throw new ResultException().message("注册失败，请稍后重试");
        }
    }

    /**
     * 渲染个人信息
     */
    public UserInfoVO get() {
        String userId = CateringUtil.getUserId();
        UserInfoVO userInfoVO = new UserInfoVO();

//        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        wrapper.eq("id", userId);
        User user = getById(userId);
        if(user == null){
            throw new ResultException().message("你已掉线，请重新登录");
        }
        BeanUtils.copyProperties(user, userInfoVO);
        if(user.getRole().equals(RoleEnum.USER_OFFLINE)){
            return userInfoVO;
        }
        Building building = buildingService.getById(user.getBuildingId());
        if(building == null){
            userInfoVO.setPark(4);
            userInfoVO.setBuilding(0);
            return userInfoVO;
        }
        userInfoVO.setPark(building.getPark().getValue());
        userInfoVO.setBuilding(building.getBuilding());

        return userInfoVO;
    }

    public void update(OnlineUserUpdateDTO onlineUserUpdateDTO) {

        String userId = CateringUtil.getUserId();
        User user = getById(userId);
        if(user == null){
            throw new ResultException().message("你已掉线，请重新登录");
        }

        BeanUtils.copyProperties(onlineUserUpdateDTO, user);
        if(user.getRole().getValue()<RoleEnum.USER_ONLINE.getValue())
            user.setRole(RoleEnum.USER_ONLINE);
        boolean b = updateById(user);
        if(!b){
            throw new ResultException().message("注册失败，请稍后重试");
        }

    }

    public boolean checkCourierGrade(String courierId) {
        User user = getById(courierId);
        // 加单数
        user.setOrderAmount(user.getOrderAmount()+1);
        if(!user.getCourierGrade().equals(GradeEnum.USER5)){
            // 还没到五级，所以检测单数并升级
            if(GradeEnum.getByType(user.getCourierGrade().getType()+1).getOrderNumber() <= user.getOrderAmount()){
                user.setCourierGrade(GradeEnum.getByType(user.getCourierGrade().getType()+1));
            }
        }
        return updateById(user);
    }

    public void updateShowInfo(UserShowInfoUpdateDTO updatePA) {
        User user = new User();
        BeanUtils.copyProperties(updatePA,user);
        updateById(user);
    }

}
